package panel.mapPanel
{
	import assists.Lang;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import main.ClientConstants;
	
	import struct.Hotel;
	import struct.Restaurant;
	import struct.Scenic;
	import struct.ScenicShop;
	
	public class MapContainer extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var map:BaiduMap;
		
		private var _wid:Number;
		private var _hei:Number;		
		
		private var centerPointX:Number = 0;
		private var centerPointY:Number = 0;
		
		private var picWid:Number = 0;
		private var picHei:Number = 0;
		
		private var scaleNum:int = 12;
		
		public function MapContainer(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
		}
		
		public function initThis(wid:Number,hei:Number):void
		{
			_wid = wid;
			_hei = hei;
			
			map = new BaiduMap(_stage,_loader,_lang,_uri);
			map.init(_wid,_hei);
			this.addChild(map);
			map.x = 0;
			map.y = 0;
		}
		
		private function getCenterPoint(infoList:Array):void
		{
			var maxX:Number = 0;
			var maxY:Number = 0;
			var minX:Number = 0;
			var minY:Number = 0;
			
			maxX = infoList[0].location[0];
			maxY = infoList[0].location[1];
			minX = infoList[0].location[0];
			minY = infoList[0].location[1];
			
			for(var i:int = 0;i < infoList.length;i++){
				if(infoList[i].location[0] >= maxX){
					maxX = infoList[i].location[0];
				}
				if(infoList[i].location[1] >= maxY){
					maxY = infoList[i].location[1];
				}
				if(infoList[i].location[0] <= minX){
					minX = infoList[i].location[0];
				}
				if(infoList[i].location[1] <= minY){
					minY = infoList[i].location[1];
				}
			}
			
			centerPointX = (maxX + minX) / 2;
			centerPointY = (maxY + minY) / 2;
			
			picWid = (maxX - minX) * 1.1;
			picHei = (maxY - minY) * 1.1;
			
			countDistance();
		}
		
		private function countDistance():void
		{
			var num1:int = int((0.056 - picWid) / .05) + 12;
			var num2:int = int((0.14 - picHei) / .05) + 12;
			
			scaleNum = (num1 < num2)?num1:num2;
			
			if(scaleNum == 0){
				scaleNum = 13;
			}
		}
		
		public function changeData(list:Array):void
		{
			getCenterPoint(list);
			
			map.moveToCenter(centerPointX,centerPointY);
			map.changeZom(scaleNum);
			
			var pathList:Array = [];
			
			for(var i:int = 0;i < list.length;i++){
				if(list[i] is Scenic){
					map.addLabel(list[i].location[0],list[i].location[1],ClientConstants.VISIT_TYPE_SCENIC,String(i + 1));
				}
				if(list[i] is ScenicShop){
					map.addLabel(list[i].location[0],list[i].location[1],ClientConstants.VISIT_TYPE_FUN,String(i + 1));
				}
				if(list[i] is Restaurant){
					map.addLabel(list[i].location[0],list[i].location[1],ClientConstants.VISIT_TYPE_EATING,String(i + 1));
				}
				if(list[i] is Hotel){
					map.addLabel(list[i].location[0],list[i].location[1],ClientConstants.VISIT_TYPE_HOTEL,String(i + 1));
				}
				pathList.push(list[i].location);
			}
			
			map.addLine(pathList);
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				map.clear();
			}
		}
	}
}